package com.tedu.egmvc8.controller;

import com.tedu.egmvc8.mapper.NoticeMapper;
import com.tedu.egmvc8.pojo.dto.NoticeAddParam;
import com.tedu.egmvc8.pojo.dto.NoticeAddQuery;
import com.tedu.egmvc8.pojo.entity.Notice;
import com.tedu.egmvc8.pojo.vo.NoticeDetailVO;
import com.tedu.egmvc8.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {
    private final NoticeMapper noticeMapper;

    public NoticeController(NoticeMapper noticeMapper) {
        this.noticeMapper = noticeMapper;
    }

    @PostMapping ("/v1/notice/add")
    @ResponseBody
    public String add(NoticeAddParam noticeAddParam){
        Notice notice=new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(100L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.inster(notice);
        return "新增资讯成功";
    }
    @GetMapping("/v1/notice/list")
    @ResponseBody
    public List<NoticeListVO> list(NoticeAddQuery noticeAddQuery){
        String title=noticeAddQuery.getTitle();
        Integer type=noticeAddQuery.getType();
        List<NoticeListVO> noticeListVOS=noticeMapper.list(title,type);
        return noticeListVOS;
    }
    @PostMapping("/v1/notice/detail")
    @ResponseBody
    public NoticeDetailVO detail(NoticeAddQuery noticeAddQuery){
        String title=noticeAddQuery.getTitle();
        Integer type=noticeAddQuery.getType();
        NoticeDetailVO noticeDetailVO=noticeMapper.detail(title,type);
        return noticeDetailVO;
    }

}
